package com.aerospace;

import com.aerospace.control.FuncBottom;
import com.aerospace.control.LyricInfo;
import com.aerospace.control.MusicList;
import com.aerospace.control.TopTitle;
import com.aerospace.event.FuncBottomEvent;
import com.aerospace.event.MusicEvent;
import com.aerospace.event.MusicListEvent;
import com.aerospace.util.CacheUtils;
import com.aerospace.model.PathUtils;
import com.aerospace.model.Player;
import com.aerospace.util.PlayerUtils;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;
import java.security.SecureRandom;


public class PlayerManager {

    private static SecureRandom random = new SecureRandom();

    /**
     *  播放测试列表
     */
    Player model;

    // 展示信息
    FuncBottom funcBottom = new FuncBottom();
    MusicList musicList = new MusicList();
    LyricInfo lyricInfo = new LyricInfo();
    TopTitle topTitle = new TopTitle();
    Stage stage;

    MusicEvent musicEvent;

    public PlayerManager(Stage stage) {
        this.stage = stage;

        init();
    }

    /**
     * 初始化
     */
    public void init() {
        // 底部操作栏事件绑定
        new FuncBottomEvent(this).bind();
        // 列表事件绑定
        new MusicListEvent(this).bind();
        // 音频文件绑定事件
        musicEvent = new MusicEvent(this);

        // TODO 这里可以删掉 没有歌曲，就听听伤痕吧
        if (musicList.isEmpty()) {
            String tmpPath = PathUtils.RESOURCES_PATH + "/你的酒馆对我打了烊.mp3";
            System.out.println("----------------");
            System.out.println("----------------");
            System.out.println("----------------");
            System.out.println(tmpPath);
            Player model = PlayerUtils.getPlayer(tmpPath);
            // 加入列表
            musicList.add(model);
            // TODO 如果去除，需要这样初始化
            // primaryStage.show();
        }

        // 获取播放记录
        if (!musicList.isEmpty()) {
            model = musicList.get(CacheUtils.getIndex());
            funcBottom.getPlayModelChoiceBox().setValue(CacheUtils.getPlayMode().getName());

            bindMusic();

            play();
        }

    }


    /**
     * 歌曲事件重新绑定
     */
    public void bindMusic() {
        musicEvent.bind();
    }

    /**
     * 下一首
     */
    public void playNext() {
        stop();
        // 获取下一首，播放
        int index = CacheUtils.getIndex();
        index += 1;
        if (index >= musicList.size()) {
            index = 0;
        }
        CacheUtils.setIndex(index);
        model = musicList.get(index);
        musicEvent.bind();

        play();
    }

    /**
     * 上一首
     */
    public void playPre() {
        stop();
        // 获取下一首，播放
        int index = CacheUtils.getIndex();
        index -= 1;
        if (index < 0) {
            index = musicList.size() - 1;
        }
        CacheUtils.setIndex(index);
        model = musicList.get(index);
        musicEvent.bind();

        play();
    }

    /**
     * 停止
     */
    public void stop() {
        if (model == null) {
            return;
        }
        getMediaPlayer().stop();
    }

    /**
     * 暂停
     */
    public void pause() {
        getMediaPlayer().pause();
    }

    /**
     * 播放
     */
    public void play() {
        getMediaPlayer().play();
    }

    /**
     * 跳到播放位置
     * @param seconds
     */
    public void seek(Duration seconds) {
        getMediaPlayer().seek(seconds);
    }

    /**
     * 播放状态
     * @return
     */
    public MediaPlayer.Status getStatus() {
        return getMediaPlayer().getStatus();
    }

    /**
     * 播放结束
     */
    public void playEnd() {
        switch (CacheUtils.getPlayMode()) {
            case CYCLING:
                // 循环，如果是最后一首，就放第一首
                playNext();
                break;
            case ONE_CYCLING:
                // 单曲循环
                seek(Duration.ZERO);
                break;
            case ORDER:
                playOrder();
                break;
            case RANDOM:
                playRandom();
                break;
            default:
                break;
        }
    }

    /**
     * 随机播放
     */
    private void playRandom() {
        stop();
        CacheUtils.setIndex(random.nextInt(musicList.size()));
        model = musicList.get(CacheUtils.getIndex());
        musicEvent.bind();
        play();
    }

    /**
     * 顺序播放
     */
    private void playOrder() {
        // 如果是最后一首，就结束
        int tmpIndex = CacheUtils.getIndex() + 1;
        if (tmpIndex >= musicList.size()) {
            seek(Duration.ZERO);
            stop();
        } else {
            playNext();
        }
    }


    public MediaPlayer getMediaPlayer() {
        return model.getMediaPlayer();
    }

    public FuncBottom getFuncBottom() {
        return funcBottom;
    }

    public MusicList getMusicList() {
        return musicList;
    }

    public LyricInfo getLyricInfo() {
        return lyricInfo;
    }

    public TopTitle getTopTitle() {
        return topTitle;
    }

    public Player getModel() {
        return model;
    }

    public void setModel(Player model) {
        this.model = model;
    }

    public Stage getStage() {
        return stage;
    }
}
